/*
 * rfm23.h
 *
 *  Created on: 01.12.2011
 *      Author: alexs
 */

#ifndef RFM23_H_
#define RFM23_H_

#define RFM23_00_Device_Type_Code_DT 0x00
#define RFM23_01_Version_Code_VC 0x01
#define RFM23_02_Device_Status 0x02
#define RFM23_03_Interrupt_Status_1 0x03
#define RFM23_04_Interrupt_Status_2 0x04
#define RFM23_05_Interrupt_Enable_1 0x05
#define RFM23_06_Interrupt_Enable_2 0x06
#define RFM23_07_Operating_Mode_and_Function_Control_1 0x07
#define RFM23_08_Operating_Mode_and_Function_Control_2 0x08
#define RFM23_09_30_MHz_Crystal_Oscillator_Load_Capacitance 0x09
#define RFM23_0A_Microcontroller_Output_Clock 0x0A
#define RFM23_0B_GPIO_Configuration_0 0x0B
#define RFM23_0C_GPIO_Configuration_1 0x0C
#define RFM23_0D_GPIO_Configuration_2 0x0D
#define RFM23_0E_I_O_Port_Configuration 0x0E
#define RFM23_0F_ADC_Configuration 0x0F
#define RFM23_10_ADC_Sensor_Amplifier_Offset 0x10
#define RFM23_11_ADC_Value 0x11
#define RFM23_12_Temperature_Sensor_Calibration 0x12
#define RFM23_13_Temperature_Value_Offset 0x13
#define RFM23_14_Wake_Up_Timer_Period_1 0x14
#define RFM23_15_Wake_Up_Timer_Period_2 0x15
#define RFM23_16_Wake_Up_Timer_Period_3 0x16
#define RFM23_17_Wake_Up_Timer_Value_1 0x17
#define RFM23_18_Wake_Up_Timer_Value_2 0x18
#define RFM23_19_Low_Duty_Cycle_Mode_Duration 0x19
#define RFM23_1A_Low_Battery_Detector_Threshold 0x1A
#define RFM23_1B_Battery_Voltage_Level 0x1B
#define RFM23_1C_IF_Filter_Bandwidth 0x1C
#define RFM23_1D_AFC_Loop_Gearshift_Override 0x1D
#define RFM23_1E_AFC_Timing_Control 0x1E
#define RFM23_1F_Clock_Recovery_Gearshift_Override 0x1F
#define RFM23_20_Clock_Recovery_Oversampling_Rate 0x20
#define RFM23_21_Clock_Recovery_Offset_2 0x21
#define RFM23_22_Clock_Recovery_Offset_1 0x22
#define RFM23_23_Clock_Recovery_Offset_0 0x23
#define RFM23_24_Clock_Recovery_Timing_Loop_Gain_1 0x24
#define RFM23_25_Clock_Recovery_Timing_Loop_Gain_0 0x25
#define RFM23_26_Received_Signal_Strength_Indicator 0x26
#define RFM23_27_RSSI_Threshold_for_Clear_Channel_Indicator 0x27
#define RFM23_28_Antenna_Diversity_1 0x28
#define RFM23_29_Antenna_Diversity_2 0x29
#define RFM23_2A_AFC_Limiter 0x2A
#define RFM23_2B_AFC_Correction_LSBs 0x2B
#define RFM23_2C_OOK_Counter_Value_1 0x2C
#define RFM23_2D_OOK_Counter_Value_2 0x2D
#define RFM23_2E_Slicer_Peak_Holder 0x2E
#define RFM23_30_Data_Access_Control 0x30
#define RFM23_31_EZMAC_Status 0x31
#define RFM23_32_Header_Control_1 0x32
#define RFM23_33_Header_Control_2 0x33
#define RFM23_34_Preamble_Length 0x34
#define RFM23_35_Preamble_Detection_Control_1 0x35
#define RFM23_36_Synchronization_Word_3 0x36
#define RFM23_37_Synchronization_Word_2 0x37
#define RFM23_38_Synchronization_Word_1 0x38
#define RFM23_39_Synchronization_Word_0 0x39
#define RFM23_3A_Transmit_Header_3 0x3A
#define RFM23_3B_Transmit_Header_2 0x3B
#define RFM23_3C_Transmit_Header_1 0x3C
#define RFM23_3D_Transmit_Header_0 0x3D
#define RFM23_3E_Packet_Length 0x3E
#define RFM23_3F_Check_Header_3 0x3F
#define RFM23_40_Check_Header_2 0x40
#define RFM23_41_Check_Header_1 0x41
#define RFM23_42_Check_Header_0 0x42
#define RFM23_43_Header_Enable_3 0x43
#define RFM23_44_Header_Enable_2 0x44
#define RFM23_45_Header_Enable_1 0x45
#define RFM23_46_Header_Enable_0 0x46
#define RFM23_47_Received_Header_3 0x47
#define RFM23_48_Received_Header_2 0x48
#define RFM23_49_Received_Header_1 0x49
#define RFM23_4A_Received_Header_0 0x4A
#define RFM23_4B_Received_Packet_Length 0x4B
#define RFM23_4F_ADC8_Control 0x4F
#define RFM23_60_Channel_Filter_Coefficient_Address 0x60
#define RFM23_62_Crystal_Oscillator_Power_on_Reset_Control 0x62
#define RFM23_69_AGC_Override_1 0x69
#define RFM23_6D_TX_Power 0x6D
#define RFM23_6E_TX_Data_Rate_1 0x6E
#define RFM23_6F_TX_Data_Rate_0 0x6F
#define RFM23_70_Modulation_Mode_Control_1 0x70
#define RFM23_71_Modulation_Mode_Control_2 0x71
#define RFM23_72_Frequency_Deviation 0x72
#define RFM23_73_Frequency_Offset_1 0x73
#define RFM23_74_Frequency_Offset_2 0x74
#define RFM23_75_Frequency_Band_Select 0x75
#define RFM23_76_Nominal_Carrier_Frequency_1 0x76
#define RFM23_77_Nominal_Carrier_Frequency_0 0x77
#define RFM23_79_Frequency_Hopping_Channel_Select 0x79
#define RFM23_7A_Frequency_Hopping_Step_Size 0x7A
#define RFM23_7C_TX_FIFO_Control_1 0x7C
#define RFM23_7D_TX_FIFO_Control_2 0x7D
#define RFM23_7E_RX_FIFO_Control 0x7E
#define RFM23_7F_FIFO_Access 0x7F

#define RFM23_clear_tx_fifo 0x01
#define RFM23_clear_rx_fifo	0x02
#define RFM23_clear_commit 0x10
#define RFM23_clear_tx() {\
	SPI_talk(WRITE,RFM23_08_Operating_Mode_and_Function_Control_2,RFM23_clear_tx_fifo);\
	SPI_talk(WRITE,RFM23_08_Operating_Mode_and_Function_Control_2,RFM23_clear_commit);\
	}
#define RFM23_clear_rx() {\
	SPI_talk(WRITE,RFM23_08_Operating_Mode_and_Function_Control_2,RFM23_clear_rx_fifo);\
	SPI_talk(WRITE,RFM23_08_Operating_Mode_and_Function_Control_2,RFM23_clear_commit);\
	}

#define RFM23_mode_READY 0x01
#define RFM23_mode_TX 0x08
#define RFM23_mode_RX 0x04

#define RFM23_to_READY() SPI_talk(WRITE,RFM23_07_Operating_Mode_and_Function_Control_1,RFM23_mode_READY);
#define RFM23_to_RX() SPI_talk(WRITE,RFM23_07_Operating_Mode_and_Function_Control_1,RFM23_mode_RX);
#define RFM23_to_TX() SPI_talk(WRITE,RFM23_07_Operating_Mode_and_Function_Control_1,RFM23_mode_TX);
#define RFM23_push(a) SPI_talk(WRITE,RFM23_7F_FIFO_Access,a)
#define RFM23_pop() SPI_talk(READ,RFM23_7F_FIFO_Access,0)

#define RFM23_isready() (SPI_talk(READ,RFM23_04_Interrupt_Status_2,0)&0x02)
#define RFM23_txcompl() (SPI_talk(READ,RFM23_03_Interrupt_Status_1,0)&0x04)
#define RFM23_receved() (SPI_talk(READ,RFM23_03_Interrupt_Status_1,0)&0x02)

#define RFM23_set_TXINT() SPI_talk(WRITE,RFM23_05_Interrupt_Enable_1,0x04);
#define RFM23_set_RXINT() SPI_talk(WRITE,RFM23_05_Interrupt_Enable_1,0x02);
#define RFM23_clear_intterrupt() {SPI_talk(READ,RFM23_03_Interrupt_Status_1,0);\
								SPI_talk(READ,RFM23_04_Interrupt_Status_2,0);}

typedef struct {
	uint8_t addr;
	uint8_t value;
} rfm_init_struct_t;

uint8_t SPI_talk(SPI_dir dir, const uint8_t addr, const uint8_t val);
void RFM23_Init(uint8_t tx);


#endif /* RFM23_H_ */
